package br.com.verde.exemploteca.phonecode

class Coder(words: List[String]) {
  private val mnemonics = Map(
    '2' -> "ABC", '3' -> "DEF", '4' -> "GHI", '5' -> "JKL",
    '6' -> "MNO", '7' -> "PQRS", '8' -> "TUV", '9' -> "WXYZ")

  // Invert the mnemonics map to give a map from chars
  // 'A' ... 'Z' to '2' ... '9'
  private val upperCode: Map[Char, Char] = {
    for {
      (digit, string) <- mnemonics
      letter <- string
    } yield (letter -> digit)
  }

  // Map a word to the digit string it can represent 
  def wordCode(word: String): String =
    word.toUpperCase map (upperCode(_))

  // A map from digit strings to the words that represents them
  val wordsForNum: Map[String, List[String]] = words groupBy wordCode

  // Return all ways to encode a number as a list of words 
  //  def encode(number: String): Set[List[String]] = {
  //    if (number.isEmpty) {
  //      Set(List())
  //    } else {
  //      for {
  //        splitPoint <- Set(1 to number.length: _*)
  //        word <- wordsForNum(number take splitPoint)
  //        rest <- encode(number drop splitPoint)
  //      } yield word :: rest
  //    }
  //  }

  // Maps a number to a list of all words phrases that can represent it
  //  def translate(number: String): Set[String] = encode(number) map (_ mkString " ")

}

object Coder {
  def main(args: Array[String]) {
    println("Phonecode Application")
    val coder = new Coder(List("JAVA", "LATA", "MUDO"))

    println(coder.wordCode("java"))
    println(coder.wordsForNum("5282"))
    //    println(coder.encode("5"))
    //    println(coder.encode("6836"))
  }
}